package com.example.demo_test.Service.Imp;

import com.example.demo_test.DTO.UserAndMessage;
import com.example.demo_test.Dao.GroupDao;
import com.example.demo_test.Dao.PersonDao;
import com.example.demo_test.Entity.Page;
import com.example.demo_test.Entity.ReqRecord;
import com.example.demo_test.Entity.Result;
import com.example.demo_test.Entity.User;
import com.example.demo_test.Service.MessageService;
import com.example.demo_test.Service.PersonService;
import com.example.demo_test.Service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhanghong
 * @date 2021/4/9  0009 17:54
 */
@Service
public class PersonServiceImp implements PersonService {
    @Autowired
    private PersonDao personDao;
    @Autowired
    private GroupDao groupDao;
    @Autowired
    private MessageService messageService;

    @Override
    public Result Login(String userId, String password) {
        if(WebSocketService.isLogin( userId )){
            return new Result(404,"用户已经登陆，你不能再登陆当前用户","");
        }
        User user= personDao.Login( userId, password );
        Result result = new Result();
        if (user == null) {
            result.setData( "" );
            result.setMessage( "账号密码错误" );
            result.setCode( 404 );
            return result;
        }
        result.setCode( 200 );
        result.setMessage( "登陆成功" );
        result.setData( user );
        return result;
    }

    @Override
    public List<User> getFriends(String userId) {
        return personDao.getFriends(userId);
    }

    @Override
    public User getUserById(String userId) {
        return personDao.getUserById(userId);
    }

    @Override
    public Result addFriend(String userId, String friendId) {
        if(userId.equals( friendId )){
            return new Result(400,"不可以添加自己为好友","");
        }
        if(personDao.getUserById( friendId )==null){
            return new Result(400,"你想加的好友未注册","");
        }
        String s=personDao.isFriend( userId,friendId );
        if(s!=null){
            return new Result(400,"你们已经是好友了，无需重新添加","");
        }
        ReqRecord reqRecord=new ReqRecord();
        reqRecord.setStatus( "待同意" );
        reqRecord.setFromUser( userId );
        reqRecord.setToUser( friendId );
        reqRecord.setContent( "请求添加好友" );
        reqRecord.setOperate( "addFriend" );
        reqRecord.setDate( new Date() );
        Result result=new Result();
        try {
            personDao.addReqRecord( reqRecord );
            WebSocketService.pushReqRecord( reqRecord );
            result.setCode( 200 );
            result.setMessage( "好友请求已经发送，等待好友同意" );
        }catch (Exception e){
            result.setCode( 404 );
            result.setMessage( "网络繁忙，请求失败了" );
        }
        return result;
    }

    @Override
    public List<ReqRecord> getReqRecordById(String userId) {
        return personDao.getReqRecordById(userId);
    }

    @Override
    public Result agreeReqRecord(ReqRecord reqRecord) {
        Result result=new Result();
        personDao.updateReqRecord( reqRecord );
        if(reqRecord.getStatus().equals( "已拒绝" )){
            try {
                result.setCode( 200 );
                result.setMessage( "拒绝请求成功" );
            }catch (Exception e){
                e.printStackTrace();
                result.setCode( 400 );
                result.setMessage( "拒绝失败成功" );
            }
            return result;
        }
        if(reqRecord.getOperate().equals( "addFriend" )){
            try {
                String s=personDao.isFriend( reqRecord.getToUser(), reqRecord.getFromUser() );
                if(s!=null){
                    return new Result(400,"你们已经是好友了,你只能拒绝请求，不会影响已建立的关系","");
                }
                personDao.addFriend( reqRecord.getToUser(), reqRecord.getFromUser() );
//                User user=personDao.getUserById( reqRecord. )
                result.setCode( 200 );
                result.setMessage( "已同意好友请求" );
            }catch (Exception e){
                e.printStackTrace();
                result.setCode( 400 );
                result.setMessage( "网络异常,同意好友请求失败" );
            }
            return result;
        }
        if(reqRecord.getOperate().equals( "groupChat" )){
            try {
                if(groupDao.isInGroup( reqRecord.getToUser(),reqRecord.getGroupId() )!=null){
                    return new Result(400,"你们已经在群里,你只能拒绝请求","");
                }
                groupDao.joinGroup( reqRecord.getToUser(),reqRecord.getGroupId(),"false" );
                result.setCode( 200 );
                result.setMessage( "已同意好友发来的加入群聊请求" );
            }catch (Exception e){
                e.printStackTrace();
                result.setCode( 400 );
                result.setMessage( "网络异常,同意好友发来的加入群聊请求失败" );
            }
            return result;
        }
        return null;
    }

    @Override
    public Result register(String userId, String password,String userName) {
        Result result=new Result();
        try {
            personDao.register( userId, password,userName );
            result.setCode( 200 );
            result.setMessage( "注册成功" );
        }catch (DuplicateKeyException e){
            e.printStackTrace();
            result.setCode( 404 );
            result.setMessage( "账号已经存在，请更换注册账号" );
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage( "网络繁忙，注册失败" );
            result.setCode( 200 );
        }
        return result;
    }

    @Override
    public Result updateUser(User user) {
        try {
            personDao.updateUser(user);
            return new Result(200,"更新个人信息信息成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙,更新个人信息信息失败","");
        }
    }

    @Override
    public List<UserAndMessage> getFriendsAndMessage(String userId) {
        List<UserAndMessage> userAndMessages=new ArrayList<>();
        List<User> userList=personDao.getFriends(userId);
        for (User user:userList
             ) {
            userAndMessages.add( new UserAndMessage(user,messageService.getMessageList( userId,user.getUserId() )) );
        }
        return userAndMessages;
    }

    @Override
    public Result changePassword(int eid, String newPassword) {
        try {
            personDao.changePassword( eid, newPassword );
            return new Result(200,"修改密码成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙，修改失败","");
        }
    }

    @Override
    public Result deleteFriend(String userId, String friendId) {
        try {
            personDao.deleteFriend(userId,friendId);
            return new Result(200,"删除好友成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙，删除失败","");
        }

    }
}
